package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ck
 * @date 2021/10/12 22:59
 */
@RestController
@RequestMapping("/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {


    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    @ApiOperation("员工登录接口")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        R<Employee> login = employeeService.login(request, employee);
        return login;
    }

    @PostMapping("/logout")
    @ApiOperation("员工退出接口")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    @ApiOperation("新增员工接口")
    @CacheEvict(value = "employeeCache",allEntries = true)
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        Long id = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(id);
//        employee.setUpdateUser(id);

        employeeService.save(employee);
        return R.success("员工添加成功");
    }

    @GetMapping("/page")
    @ApiOperation("员工分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "员工姓名",required = false)
    })
    @Cacheable(value = "employeeCache",key = "#page+'_'+#pageSize")
    public R<Page> page(int page,int pageSize,String name){
        Page<Employee> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(name),Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    @PutMapping
    @ApiOperation("修改员工数据接口")
    @CacheEvict(value = "employeeCache",allEntries = true)
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
//        Long id = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(id);
//        employee.setUpdateUser(id);

        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据员工id查询员工接口")
    @Cacheable(value = "employeeCache",key = "#id")
    public  R<Employee> getById(@PathVariable Long id){
        Employee emp = employeeService.getById(id);
        return R.success(emp);
    }
}
